# coding=utf-8
'''
3) 自动取款机
   3.1 账户数据源构造
   3.2 登陆功能 （注册功能）
   3.3 查询余额功能
   3.4 取钱功能
   3.5 存钱功能
   3.6 退出功能
   3.7 界面的交互功能
'''

list_user = [{'name': 'zhangsan', 'pwd': '123456', 'balance': 10000},
             {'name': 'lisi', 'pwd': '123456', 'balance': 20000},
             {'name': 'wangwu', 'pwd': '123456', 'balance': 30000}]


# 登陆功能
def login():
    index = 5
    while index >= 1:
        name = input("请输入用户名：")
        password = input("请输入密码：")
        # 验证用户信息
        user = check_user_info(name, password)
        if user is not None:
            print('='*52)
            print("-"*15,"欢迎{}登陆成功".format(user['name']),"-"*15)
            make_page(name,password)
            return False
        else:
            index = index - 1
            if index == 0:
                print("连续输入5次用户名密码错误，账号被锁!")
                return False
            else:
                print("用户名或密码错误")
                input("你还有{}次机会,按回车请重新输入!".format(index))
                print('=' * 52)



# 验证用户信息
def check_user_info(name, password):
    for user in list_user:
        if name == user['name']:
            if password == user['pwd']:
                return user
            else:
                return None
            continue
        else:
            continue
            return None


# login()

# 注册新用户
def add_user_info():
    print("-" * 20, '存款操作界面', "-" * 20)
    name = input('请输入要注册的用户名：')
    mark = True
    while mark:
        password1 = input('请设置你的取款密码：')
        password2 = input('请再次确认你的密码：')
        if password1 != password2:
            input("两次输入的密码不一致，请按回车重新输入!")
            print('=' * 52)
        elif len(password1) == 0:
            input("密码不能为空，请按回车重新输入!")
            print('=' * 52)
        else:
            mark = False
    dict = {'name': name, 'pwd': password2,'balance':0}
    list_user.append(dict)


#
# add_user_info()
# print(list_user)

# 查询账户余额信息
def query_user_balance(name, password):
    user = check_user_info(name, password)
    if user is not None:
        return user['balance']
    else:
        return None


# print(query_user_balance('wangwu', '123456'))


def take_out_money(name, password, num):
    user = check_user_info(name, password)
    if user is not None:
        if num <= user['balance']:
            user['balance'] = user['balance'] - num
            print("取款{}元，账户余额：{}元".format(num,user['balance']))
            print("")
        elif num > user['balance']:
            print("取款金额不能大于账户余额:{}元".format(user['balance']))
            print("")


# take_out_money('zhangsan','123456',100200)
# print(list_user)

def take_in_money(name, password, num):
    user = check_user_info(name, password)
    if user is not None:
        if num > 0:
            user['balance'] = user['balance'] + num
            print("存入{}元，账户余额：{}元".format(num, user['balance']))
            print("")
        elif num < 0:
            print("存款金额不能小于等于0")
            print("")


# take_in_money('zhangsan','123456',100200)
# print(list_user)

def logout():
    pass

#输入整数的方法
def input_int(content='请输入100的整数倍:'):
    while True:
        data = input(content)
        try:
            inputInt = int(data)
            if inputInt % 100 == 0:
                return inputInt
        except:
            pass
# print(input_int())

#操作页面
def make_page(name, password):
    while True:
        print('{0:<4}{1:<28}{2:<15}'.format(' ', '1.查询余额', '2.取款'))
        print('{0:<4}{1:<30}{2:<15}'.format(' ', '3.存款', '4.退出系统'))
        print("=" * 52)
        num = input("请输入对应操作序号：")
        if num == '1':
            print("-"*19,'账户余额信息',"-"*19)
            money= query_user_balance(name, password)
            print("余额：{}".format(money))

        elif num == '2':
            print("-" * 19, '取款操作界面', "-" * 19)
            money=input_int()
            take_out_money(name, password, money)

        elif num == '3':
            print("-" * 19, '存款操作界面', "-" * 19)
            money = input_int()
            take_in_money(name, password, money)

        elif num == '4':
            logout()
            print("-" * 14, '感谢使用自动取款系统！', "-" * 14)
            break


#初始登陆界面
while True:
    print("="*20,'自助取款机','='*20)
    print('{0:3}{1:<10}{2:8}{3:<10}'.format(" ", "1.账号登陆", " ", "2.账号注册"))
    print("=" * 52)
    num=input("请输入对应操作序号：")
    if num =='1':
        mark = login()
        if mark is False:
            break
    elif num == '2':
        add_user_info()




